package org.esti.service.impl;

import org.esti.model.Appraisal;
import org.esti.model.AppraisalTask;
import org.esti.model.KnowledgeField;
import org.esti.model.dao.AppraisalDAO;
import org.esti.model.dao.AppraisalTaskDAO;
import org.esti.service.AppraisalTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Artem Loginov (logart)
 */

/**
 * {@inheritDoc}
 */
@Service
public class AppraisalTaskServiceImpl implements AppraisalTaskService {

    @Autowired
    private AppraisalDAO appraisalDAO;

    @Autowired
    private AppraisalTaskDAO appraisalTaskDAO;

    /**
     * {@inheritDoc}
     */
    @Override
    public Map<KnowledgeField, List<AppraisalTask>> getAppraisalTasksByLevelId(long levelId) {
        Appraisal appraisal = appraisalDAO.getAppraisalByLevelId(levelId);
        List<AppraisalTask> appraisalTasks = appraisalTaskDAO.getAppraisalsTaskByAppraisalId(appraisal.getId());
        Map<KnowledgeField, List<AppraisalTask>> tasksByKnowledge = new HashMap<KnowledgeField, List<AppraisalTask>>();

        for (AppraisalTask task : appraisalTasks) {
            if (tasksByKnowledge.containsKey(task.getKnowledgeField())) {
                tasksByKnowledge.get(task.getKnowledgeField()).add(task);
            } else {
                List<AppraisalTask> appraisalTasksByKnowledge = new ArrayList<AppraisalTask>();
                appraisalTasksByKnowledge.add(task);
                tasksByKnowledge.put(task.getKnowledgeField(), appraisalTasksByKnowledge);
            }
        }

        return tasksByKnowledge;
    }
}
